//
//  InsuranceBankViewController.m
//  PhotoAnnotate
//
//  Created by Bassem El-Tamimi on 21.02.11.
//  Copyright 2011 ePapyrus GmbH. All rights reserved.
//

#import "InsuranceBankViewController.h"
#import "Insurance.h"
#import "InsuranceViewController.h"


#define FEATURES_TAB 1
#define INSURE_TAB 2
#define STATUS_TAB 3

@implementation InsuranceBankViewController

@synthesize insuranceData;
@synthesize insuranceViewController;
@synthesize bankRoutingNumbersDE;
@synthesize bankRoutingNumbersAT;
@synthesize paymentPeriods;
@synthesize paymentPeriodKeys;


#pragma mark -
#pragma mark view lifecycle methods

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = NSLocalizedString(@"INSURANCE_BANK_TITLE", @"Bank Information");
	self.navigationController.navigationBar.translucent = NO;
	self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
	
	// provide Save and Cancel buttons
	UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
																			  target:self action:@selector(saveFieldData:)];
	
	UIBarButtonItem* cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
																				target:self action:@selector(cancel:)];
	self.navigationItem.rightBarButtonItem = saveItem;
	self.navigationItem.leftBarButtonItem = cancelItem;
	
	[saveItem release];
	[cancelItem release];
	
	[bankRoutingNumberField setPlaceholder:NSLocalizedString(@"INSURANCE_BANK_PLACEHOLDER_ROUTING_NUMBER", @"Bank Routing Number")];
	[bankNameField setPlaceholder:NSLocalizedString(@"INSURANCE_BANK_PLACEHOLDER_NAME", @"Bank Name")];
	[bankAccountNumberField setPlaceholder:NSLocalizedString(@"INSURANCE_BANK_PLACEHOLDER_ACCOUNT_NUMBER", @"Account Number")];
	[bankAccountNameField setPlaceholder:NSLocalizedString(@"INSURANCE_BANK_PLACEHOLDER_ACCOUNT_NAME", @"Account Name")];
	
	if ([insuranceData.bankConnection objectForKey:@"RoutingNumber"]) {
		[bankRoutingNumberField setText:[insuranceData.bankConnection objectForKey:@"RoutingNumber"]];
	}
	
	if ([insuranceData.bankConnection objectForKey:@"BankName"]) {
		[bankNameField setText:[insuranceData.bankConnection objectForKey:@"BankName"]];
	}
	
	if ([insuranceData.bankConnection objectForKey:@"AccountNumber"]) {
		[bankAccountNumberField setText:[insuranceData.bankConnection objectForKey:@"AccountNumber"]];
	}
	
	if ([insuranceData.bankConnection objectForKey:@"AccountName"]) {
		[bankAccountNameField setText:[insuranceData.bankConnection objectForKey:@"AccountName"]];
	}
	
	
	bankRoutingNumbersDE = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BLZ_20110307" ofType:@"plist"]];
	bankRoutingNumbersAT = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"kiverzeichnis_gesamt_at" ofType:@"plist"]];
	
	self.paymentPeriods = [NSArray arrayWithObjects:NSLocalizedString(@"INSURANCE_PAYMENT_PERIOD_MONTHLY", @"Monthly"), 
						   NSLocalizedString(@"INSURANCE_PAYMENT_PERIOD_QARTERLY", @"Quarterly"), 
						   NSLocalizedString(@"INSURANCE_PAYMENT_PERIOD_BIANNUALLY", @"Biannually"), 
						   NSLocalizedString(@"INSURANCE_PAYMENT_PERIOD_ANNUALLY", @"Annually"), 
						   nil];
	
	
	NSValue *aOne = [NSNumber numberWithInt:1];
	NSValue *aThree = [NSNumber numberWithInt:3];
	NSValue *aSix = [NSNumber numberWithInt:6];
	NSValue *aTwelve = [NSNumber numberWithInt:12];

	self.paymentPeriodKeys = [NSArray arrayWithObjects:aOne, aThree, aSix, aTwelve, nil];
	
	[paymentPeriodPicker selectRow:[self.paymentPeriodKeys indexOfObject:[self.insuranceData paymentPeriod]] inComponent:0 animated:YES];
	
	[paymentPeriodLabel setText:NSLocalizedString(@"INSURANCE_PAYMENT_PERIOD_PICKER_LABEL", @"Preferred Payment Period")];

}

- (void)cancel:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
	
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	[self.navigationController setNavigationBarHidden:NO animated:YES];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)viewWillDisappear:(BOOL)animated
{	
	[super viewWillDisappear:animated];
	
	[self.navigationController setNavigationBarHidden:YES animated:NO];
	
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (void)saveFieldData:(id)sender
{
		
	[insuranceData.bankConnection setObject:bankRoutingNumberField.text forKey:@"RoutingNumber"];
	
	[insuranceData.bankConnection setObject:bankNameField.text forKey:@"BankName"];
	
	[insuranceData.bankConnection setObject:bankAccountNumberField.text forKey:@"AccountNumber"];
	
	[insuranceData.bankConnection setObject:bankAccountNameField.text forKey:@"AccountName"];

	// No need to save the picker value since it is saved in the delegate method
	
	[self.insuranceData updatePropertyListData];
	[self.insuranceData saveChanges];
	
	[self.navigationController popViewControllerAnimated:YES];
	
}


#pragma mark -
#pragma mark textField delegate methods

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
	
	if (textField.tag == 1 && [bankNameField.text length] == 0) {
		
		if ([textField.text length] == 8) {
			if ([bankRoutingNumbersDE objectForKey:bankRoutingNumberField.text] != nil) {
				bankNameField.text = [bankRoutingNumbersDE objectForKey:bankRoutingNumberField.text];
			}
		} else {
			if ([bankRoutingNumbersAT objectForKey:bankRoutingNumberField.text] != nil) {
				bankNameField.text = [bankRoutingNumbersAT objectForKey:bankRoutingNumberField.text];
			}
		}

	}
	
	return YES;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	
	[textField resignFirstResponder];
	return YES;
}

#pragma mark -
#pragma mark UIPickerViewDelegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	self.insuranceData.paymentPeriod = (NSNumber *)[self.paymentPeriodKeys objectAtIndex:row];
}


#pragma mark -
#pragma mark UIPickerViewDataSource

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	return [self.paymentPeriods objectAtIndex:row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	CGFloat componentWidth = 280.0;
	
	return componentWidth;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
	return 40.0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return [paymentPeriods count];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}


@end
